import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:glorysoft_mobile_plugin/tool/extensions/extension.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/lot_action_entity.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/bean/lot_id_entity_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';

import 'EndWorkNextOneState.dart';

class EndWorkNextDefectController extends BaseController {
  final EndWorkNextOneState state = EndWorkNextOneState();

  @override
  void onInit() {
    super.onInit();
    state.lotEntity = Get.arguments;
    state.scrapCodeList
        .addAll(state.lotEntity?.DEFECTCODELIST ?? <LotSCRAPCODELIST>[]);
    state.qtyController.text = state.lotEntity!.mAINQTY.toString();
  }

  next(){
    List<LotIdEntity> data = [];
    LotIdEntity info = LotIdEntity();
    info.lOTID = state.lotEntity!.lOTID ?? '';
    info.MAINQTY = state.lotEntity?.mAINQTY ?? 0;
    data.add(info);

    List<LotActionEntity> entity = [];
    state.defectList.forEach((element) {
      LotActionEntity mEntity = LotActionEntity();
      mEntity.lOTID = state.lotEntity!.lOTID ?? '';
      mEntity.aCTIONCODE = element.cODE;
      mEntity.aCTIONTYPE = 'Defect';
      mEntity.aCTIONCOMMENT = element.dESCRIPTION;
      List<LotActionQTYLIST> mList = [];
      LotActionQTYLIST mQTY = LotActionQTYLIST();
      mQTY.mAINQTY = (element.DEFECTQTY == null ? '0.0' : element.DEFECTQTY)!.toDouble();
      mList.add(mQTY);
      mEntity.qTYLIST = mList;
      entity.add(mEntity);
    });

    Get.toNamed(Routes.EndWorkNextTwo,arguments: {
      'LOTLIST' : data,
      'DURABLEIDLIST' : state.carryList,
      'LOTACTIONLIST' : entity,
      'LOTENTITY' : state.lotEntity,
      'DEFECTCOUNT' : state.defectCount
    });
  }

  trackOut() async{
    List<LotIdEntity> data = [];
    LotIdEntity info = LotIdEntity();
    info.lOTID = state.lotEntity!.lOTID ?? '';
    data.add(info);

    List<LotActionEntity> entity = [];
    state.defectList.forEach((element) {
      LotActionEntity mEntity = LotActionEntity();
      mEntity.lOTID = state.lotEntity!.lOTID ?? '';
      mEntity.aCTIONCODE = element.cODE;
      mEntity.aCTIONTYPE = 'Scrap';
      mEntity.aCTIONCOMMENT = element.dESCRIPTION;
      List<LotActionQTYLIST> mList = [];
      LotActionQTYLIST mQTY = LotActionQTYLIST();
      mQTY.mAINQTY = (element.DEFECTQTY == null ? '0.0' : element.DEFECTQTY)!.toDouble();
      mList.add(mQTY);
      mEntity.qTYLIST = mList;
      entity.add(mEntity);
    });
    Map<String, dynamic> params = {'ACTIONTYPE':'DefaultTrackOut', 'LOTLIST':data,'DURABLEIDLIST':state.carryList,'LOTACTIONLIST' : entity};
    var result = await LotApi.trackOut(canelToken,params: params);
    if(result.ok){
      Fluttertoast.showToast(
        msg: "出站成功",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      );

      Get.offAllNamed(Routes.Tabs);
    }
  }

  void addCarryData(String str) {
    state.carryList.add(str);
    update([state.carryLogicId, state.carrierLogicId]);
  }

  void deleteCarryData(String str) {
    state.carryList.remove(str);
    update([state.carryLogicId]);
  }

  void updateSelectDefectValue(LotSCRAPCODELIST str) {
    state.selectDefectValue = str;
    update([state.defectCodeLogicId]);
  }

  void updateDefectCount() {
    double count = 0;
    state.defectList.forEach((element) {
      count += double.parse(element.DEFECTQTY ?? '0.0');
    });
    state.defectCount = count;
    update([state.defectCountLogicId]);
  }

  void addDefectData() {
    if (state.selectDefectValue == null) {
      Fluttertoast.showToast(
        msg: "请选择缺陷代码",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      );
    } else {
      LotSCRAPCODELIST defectBean = LotSCRAPCODELIST();
      defectBean.cODE = state.selectDefectValue?.cODE ?? "";
      defectBean.dESCRIPTION = state.selectDefectValue?.dESCRIPTION ?? "";
      state.defectList.add(defectBean);
      update([state.defectTableLogicId, state.defectCountLogicId]);
    }
  }
}
